package com.google.sydym6.logic.managers.recognizing;

import android.util.Log;

import com.google.sydym6.logic.exceptions.LastfmLoginException;
import com.google.sydym6.logic.utils.Constants;

import de.umass.lastfm.Authenticator;
import de.umass.lastfm.Caller;
import de.umass.lastfm.Session;
import de.umass.lastfm.Track;

public class Scrobbler implements IScrobbler {

	private Session lastfmSession;
	private boolean enableScrobbler;
	
	public Scrobbler(String login, String password) throws LastfmLoginException {
		Caller.getInstance().setUserAgent(Constants.LASTFM_USERAGENT);
		Caller.getInstance().setCache(null);
		
		if (login != null && password != null) {
			lastfmSession = Authenticator.getMobileSession(login, password, Constants.LASTFM_API_KEY, Constants.LASTFM_SECRET);

			if (!Caller.getInstance().getLastResult().isSuccessful()) {
				throw new LastfmLoginException(Caller.getInstance().getLastResult().getErrorMessage());
			}
		}
		
		Log.v("Last Fm", "Last fm session: " + lastfmSession);
	}
	
	@Override
	public boolean hasLastFmAccount() {
		return lastfmSession != null;
	}
	
	@Override
	public void setEnableScrobbler(boolean enabled) {
		enableScrobbler = enabled;
	}
	
	@Override
	public boolean isScrobblerEnabled() {
		return enableScrobbler;
	}
	
	@Override
	public void scrobble(String artist, String title) {
		if (lastfmSession != null && enableScrobbler) {
			int now = (int) (System.currentTimeMillis() / 1000);
			Track.scrobble(artist, title, now, lastfmSession);
		}
	}

}
